package com.qf.controller;

import com.qf.Constant.RabbitMqConstant;
import com.qf.entity.Goods;
import com.qf.result.Result;
import com.qf.service.GoodsService;
import com.qf.vo.GoodsEntity;
import com.qf.vo.Stock;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/08/15:36
 * @Description:
 */
@RequestMapping("/goodsController")
@Controller
public class goodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/goodsList")
    public String getGoodsList(ModelMap modelMap){
        List<GoodsEntity> goodsList = goodsService.list();
        modelMap.put("goodsList",goodsList);
        return "goods/goodList";
    }
    @GetMapping("/{page}.html")
    public String toPage(@PathVariable String page) {
        // 此时page就是addUser，userList,xxxx
        return "goods/" + page; // user/addUser,user/userList
    }
    @PostMapping("/addGoods")
    @ResponseBody
    public Result addGoods(Goods goods){
        Goods goods1 = goodsService.saveGoodsAndStock(goods);
        rabbitTemplate.convertAndSend(RabbitMqConstant.SHOP_EXCHANGE,"shop.add",goods1);
       /* GoodsEntity entity = goodsService.save(goodsEntity);
        Integer gid = entity.getId();
        Integer stock = entity.getStock();

        Stock stock1 = new Stock(gid, stock);
        goodsService.addStock(stock1);*/
        return Result.success();
    }


}
